package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Community;
import com.atguigu.entity.House;
import com.atguigu.entity.HouseBroker;
import com.atguigu.entity.HouseImage;
import com.atguigu.result.Result;
import com.atguigu.service.CommunityService;
import com.atguigu.service.HouseBrokerService;
import com.atguigu.service.HouseImageService;
import com.atguigu.service.HouseService;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/house")
public class HouseController {

    @Reference
    private HouseService houseService;
    @Reference
    private CommunityService communityService;
    @Reference
    private HouseBrokerService houseBrokerService;
    @Reference
    private HouseImageService houseImageService;

    @RequestMapping("list/{pageNum}/{pageSize}")
    public Result list(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @RequestBody HouseQueryVo houseQueryVo) {
        PageInfo<HouseVo> page = houseService.findListPage(pageNum, pageSize, houseQueryVo);
        return Result.ok(page);
    }

    @RequestMapping("info/{id}")
    public Result info(@PathVariable Long id) {
//        以map的形式返回
        Map map = new HashMap<>();
//    获取房子信息
        House house = houseService.getById(id);

        map.put("house", house);
//    获取小区信息
        Community community = communityService.getById(house.getCommunityId());
        map.put("community", community);
//    获取经纪人信息
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(id);
        map.put("houseBrokerList", houseBrokerList);
//    获取图片信息
        List<HouseImage> houseImage1List = houseImageService.findList(id, 1);
        map.put("houseImage1List", houseImage1List);

//    获取关注信息
        map.put("isFollow", false);
//    返回信息
        return Result.ok(map);
    }
}
